package com.zhika.zishi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.adapter.FragmentSelfSPAdapter;
import com.zhika.zishi.beans.MySelfSPBean;
import com.zhika.zishi.constant.OthersConstant;
import com.zhika.zishi.customview.TitleView;
import com.zhika.zishi.fragments.MySelfSPLDFragment;
import com.zhika.zishi.fragments.set.SettingFragment;
import com.zhika.zishi.interfaces.TitleInterface;
import com.zhika.zishi.util.ActivityCollector;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/14.
 */
public class MySelfInfoActivity extends FragmentActivity implements View.OnClickListener ,TitleInterface{

    private final String TAG = "MySelfInfoActivity";

    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    private ViewPager viewPager;

    private TextView strategyText;
    private TextView problemText;
    private TextView likeText;
    private TextView draftText;

    private ArrayList<MySelfSPBean> mySelfSList = new ArrayList<>();
    private ArrayList<MySelfSPBean> mySelfSList2 = new ArrayList<>();
    private ArrayList<MySelfSPBean> mySelfSList3 = new ArrayList<>();
    private ArrayList<MySelfSPBean> mySelfSList4 = new ArrayList<>();

    //横线所在的布局
    private LinearLayout lineLayout;

    private LinearLayout.LayoutParams lineLayoutParams;

    private FragmentSelfSPAdapter mySelfSPAdapter;

    //记录屏幕的宽度
    private int screenWidth;

    //记录当前页卡位置
    private int currentPosition = 0;

    //顶部菜单
    private TitleView titleView;

    private ImageView headerImageView;

    private LinearLayout fanLayout;

    private LinearLayout likeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myself_info_layout);
        ActivityCollector.addActivity(this);
        addTestData();

        initView();
    }

    private void initView(){

        headerImageView = (ImageView) findViewById(R.id.myself_info_header_image_view);
        headerImageView.setOnClickListener(this);

        strategyText = (TextView) findViewById(R.id.user_tabs_strategy_text);
        strategyText.setOnClickListener(this);
        problemText = (TextView) findViewById(R.id.user_tabs_problem_text);
        problemText.setOnClickListener(this);
        likeText =(TextView) findViewById(R.id.user_tabs_like_text);
        likeText.setOnClickListener(this);
        draftText =(TextView) findViewById(R.id.user_tabs_draft_text);
        draftText.setOnClickListener(this);

        titleView = (TitleView) findViewById(R.id.myself_info_title_view);
        titleView.setBackgroundAlpha(R.color.color_transparent);
        titleView.setTitleViewInterface(this);
        titleView.setMiddleRowText("用户名");
        titleView.setLeftButtonText("返回");
        titleView.setRightButtonText("设置");

        //获得屏幕的宽度
        getScreenWidth();

        lineLayout = (LinearLayout) findViewById(R.id.user_tabs_line_layout);
        lineLayoutParams = (LinearLayout.LayoutParams) lineLayout.getLayoutParams();
        lineLayout.post(new Runnable() {
            @Override
            public void run() {
                lineLayoutParams.width = screenWidth/4;
                lineLayout.setLayoutParams(lineLayoutParams);
            }
        });

        viewPager = (ViewPager) findViewById(R.id.myself_info_view_pager);

        mySelfSPAdapter = new FragmentSelfSPAdapter(getSupportFragmentManager(),fragmentList);

        viewPager.setAdapter(mySelfSPAdapter);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                if (currentPosition == 0 && position == 0)// 0->1
                {
                    lineLayoutParams.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 4) + currentPosition
                            * (screenWidth / 4));

                } else if (currentPosition == 1 && position == 0) // 1->0
                {
                    lineLayoutParams.leftMargin = (int) (-(1 - positionOffset)
                            * (screenWidth * 1.0 / 4) + currentPosition
                            * (screenWidth / 4));

                } else if (currentPosition == 1 && position == 1) // 1->2
                {
                    lineLayoutParams.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 4) + currentPosition
                            * (screenWidth / 4));
                } else if (currentPosition == 2 && position == 1) // 2->1
                {
                    lineLayoutParams.leftMargin = (int) (-(1 - positionOffset)
                            * (screenWidth * 1.0 / 4) + currentPosition
                            * (screenWidth / 4));
                }
                else if (currentPosition == 2 && position == 2) //2->3
                {
                    lineLayoutParams.leftMargin = (int) (positionOffset * (screenWidth* 1.0/4)+currentPosition * (screenWidth / 4));

                }else if (currentPosition ==3 && position == 2) //3->2
                {
                    lineLayoutParams.leftMargin = (int) (-(1-positionOffset)*(screenWidth * 1.0 /4)+currentPosition * (screenWidth/4));

                }
                lineLayout.setLayoutParams(lineLayoutParams);

            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        fanLayout = (LinearLayout) findViewById(R.id.myself_info_fan_layout);
        fanLayout.setOnClickListener(this);
        likeLayout = (LinearLayout) findViewById(R.id.myself_info_like_layout);
        likeLayout.setOnClickListener(this);

    }

    private void addTestData(){

        addTestData1();
        MySelfSPLDFragment strategyFragment = new MySelfSPLDFragment();
        Bundle bundle = new Bundle();
        bundle.putString(OthersConstant.MYSELF_TYPE,"strategy");
        bundle.putSerializable(OthersConstant.KEY_2,mySelfSList);
        strategyFragment.setArguments(bundle);
        fragmentList.add(strategyFragment);

        addTestData2();
        MySelfSPLDFragment strategyFragment2 = new MySelfSPLDFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putString(OthersConstant.MYSELF_TYPE,"problem");
        bundle2.putSerializable(OthersConstant.KEY_2,mySelfSList2);
        strategyFragment2.setArguments(bundle2);
        fragmentList.add(strategyFragment2);

        addTestData3();
        MySelfSPLDFragment strategyFragment3 = new MySelfSPLDFragment();
        Bundle bundle3 = new Bundle();
        bundle3.putString(OthersConstant.MYSELF_TYPE,"like");
        bundle3.putSerializable(OthersConstant.KEY_2,mySelfSList3);
        strategyFragment3.setArguments(bundle3);
        fragmentList.add(strategyFragment3);

        addTestData4();
        MySelfSPLDFragment strategyFragment4 = new MySelfSPLDFragment();
        Bundle bundle4 = new Bundle();
        bundle4.putString(OthersConstant.MYSELF_TYPE,"draft");
        bundle4.putSerializable(OthersConstant.KEY_2,mySelfSList4);
        strategyFragment4.setArguments(bundle4);
        fragmentList.add(strategyFragment4);
    }

    private void addTestData1(){
        for (int i = 0;i<10;i++){
            MySelfSPBean bean = new MySelfSPBean();
            bean.setType("strategy");
            bean.setTitleNameStr("攻略标题攻略标题");
            bean.setCommentNumStr("11");
            bean.setCollectNumStr("22");
            bean.setLikeNumStr("33");

            mySelfSList.add(bean);

        }
    }

    private void addTestData2(){
        for (int i = 0;i<10;i++){
            MySelfSPBean bean = new MySelfSPBean();
            bean.setType("problem");
            bean.setTitleNameStr("2问题标题问题标题");
            bean.setCommentNumStr("11");
            bean.setCollectNumStr("22");

            mySelfSList2.add(bean);

        }
    }

    private void addTestData3(){
        for (int i = 0;i<5;i++){
            MySelfSPBean bean = new MySelfSPBean();
            bean.setType("strategy");
            bean.setTitleNameStr("3攻略标题攻略标题");
            bean.setCommentNumStr("11");
            bean.setCollectNumStr("22");
            bean.setLikeNumStr("33");
            bean.setDateStr("8-09");
            bean.setTimeStr("12:10");

            mySelfSList3.add(bean);
        }

        for (int i = 0;i<2;i++){
            MySelfSPBean bean = new MySelfSPBean();
            bean.setType("problem");
            bean.setTitleNameStr("问题题目");
            bean.setCommentNumStr("11");
            bean.setCollectNumStr("22");
            bean.setDateStr("5-12");
            bean.setTimeStr("1:10");

            mySelfSList3.add(bean);
        }

        for (int i = 0;i<3;i++){
            MySelfSPBean bean = new MySelfSPBean();
            bean.setType("QA");
            bean.setTitleNameStr("问题题目");

            mySelfSList3.add(bean);
        }
    }

    private void addTestData4(){
        for (int i = 0;i<3;i++){
            MySelfSPBean bean = new MySelfSPBean();
            bean.setType("draft_strategy");
            bean.setTitleNameStr("4攻略标题攻略标题");
            bean.setDateStr("1-12");
            bean.setTimeStr("12:10");

            mySelfSList4.add(bean);
        }

        for (int i = 0;i<3;i++){
            MySelfSPBean bean = new MySelfSPBean();
            bean.setType("draft_qa");
            bean.setTitleNameStr("问题题目");

            mySelfSList4.add(bean);
        }
    }

    /**
     * 得到屏幕的宽度
     * @return
     */
    private void getScreenWidth(){
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        screenWidth = displayMetrics.widthPixels;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.user_tabs_strategy_text:
                viewPager.setCurrentItem(0);
                break;
            case R.id.user_tabs_problem_text:
                viewPager.setCurrentItem(1);
                break;
            case R.id.user_tabs_like_text:
                viewPager.setCurrentItem(2);
                break;
            case R.id.user_tabs_draft_text:
                viewPager.setCurrentItem(3);
                break;
            case R.id.myself_info_header_image_view:
                headerImageViewListener();
                break;
            case R.id.myself_info_fan_layout:
                fanLikeLayoutListener("我的粉丝");
                break;
            case R.id.myself_info_like_layout:
                fanLikeLayoutListener("我的关注");
                break;

        }
    }

    @Override
    public void leftTitleListener() {
        finish();
    }

    @Override
    public void middleTitleListener() {

    }

    @Override
    public void rightTitleListener() {
        Intent intent = new Intent(MySelfInfoActivity.this,SettingActivity.class);
        startActivity(intent);
    }

    /**
     * 头像的点击事件
     */
    private void headerImageViewListener(){
        Intent intent = new Intent(MySelfInfoActivity.this,ModifySelfInfoActivity.class);
        startActivity(intent);
    }

    /**
     * 粉丝或关注的点击事件
     * type 是代表类型，即粉丝还是关注
     */
    private void fanLikeLayoutListener(String type){
        Intent intent = new Intent(MySelfInfoActivity.this,FanLikeActivity.class);
        intent.putExtra("fan_like",type);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        ActivityCollector.remoteActivity(this);
        super.onDestroy();
    }
}
